package cn.yi.newsys.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String charEncoding = "UTF-8" ;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset="+charEncoding) ;
		System.out.println("这是我的第一个Servlet");
		PrintWriter out = response.getWriter() ;
		out.print("这是我的第一个Servlet") ;
		out.flush() ;
		out.close() ;
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

	@Override
	public void destroy() {
		System.out.println("销毁的方法只执行一次");
	}

	
	// 存在两个init 方法时，先去配置文件中看有没有初始化参数，有则执行带参数的 init 方法，没有则执行不带参数的 init 方法
	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("初始化的方法只执行一次(初始化的步骤②)");
		// 读取 web.xml 中的初始化参数
		charEncoding = config.getInitParameter("charEncoding") ;
		System.out.println( charEncoding );
	}
	@Override
	public void init() throws ServletException {
		System.out.println("初始化的方法只执行一次(初始化的步骤①)");
	}
	
	
}
